package org.android;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.helloroad.Trip;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class TripInfoView extends Activity {
	private TextView departure;
	private TextView arrival;
	private TextView date;
	private TextView time;
	private Button map;
	
	private double dLatitude;
	private double dLongitude;

	private double aLatitude;
	private double aLongitude;
	
	private List<Address> addressDepartureResult;
	private Address coord;

	@SuppressWarnings("unused")
	private static final int DRIVER = Menu.FIRST;
	@SuppressWarnings("unused")
	private static final int OCCUPANT = Menu.FIRST + 1;
	private static final int ACTIVITY_CREATE = 0;
	
	private static final int ADDRESS_DIALOG_ID = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.trip_view);
		
		departure = (TextView) findViewById(R.id.tripDeparture);
		arrival = (TextView) findViewById(R.id.tripArrival);
		date = (TextView) findViewById(R.id.tripDate);
		time = (TextView) findViewById(R.id.tripTime);
		map = (Button) findViewById(R.id.btnFindOnMap);
		Bundle extras = getIntent().getExtras();
		
		 if (extras != null) {
			Trip tripDetails = (Trip) extras.get("tripDetails");

			departure.setText(tripDetails.getFrom());
			arrival.setText(tripDetails.getTo());
			if (tripDetails.getDate() != null
					&& tripDetails.getDate().equals("") != true)
				date.setText(tripDetails.getDate());
			else {
				date.setText(displayFrequency(tripDetails));
			}
			time.setText(tripDetails.getTime());
			map.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					goToMap();
				}
			});	 
		 }
	}

	private String displayFrequency(Trip tripDetails) {
		StringBuilder str = new StringBuilder();
		if (tripDetails.monday == true)
			str.append("monday, ");
		if (tripDetails.tuesday == true)
			str.append("tuesday, ");
		if (tripDetails.wednesday == true)
			str.append("wednesday, ");
		if (tripDetails.thursday == true)
			str.append("thursday, ");
		if (tripDetails.friday == true)
			str.append("friday, ");
		if (tripDetails.saturday == true)
			str.append("saturday, ");
		if (tripDetails.sunday == true)
			str.append("sunday");
		return str.toString();
	}

	protected void goToMap() {
		Geocoder geocoder = new Geocoder(this, Locale.getDefault());
		List<Address> la;
		try {
			la = geocoder
					.getFromLocationName(departure.getText().toString(), 1);
			Address tmp = la.get(0);
			dLatitude = tmp.getLatitude();
			dLongitude = tmp.getLongitude();

			la = geocoder.getFromLocationName(arrival.getText().toString(), 1);
			tmp = la.get(0);
			aLatitude = tmp.getLatitude();
			aLongitude = tmp.getLongitude();

		} catch (IOException e) {
			e.printStackTrace();
		}

		Intent i = new Intent(this, GoogleMapView.class);
		i.putExtra("dLatitude", dLatitude);
		i.putExtra("dLongitude", dLongitude);
		i.putExtra("aLatitude", aLatitude);
		i.putExtra("aLongitude", aLongitude);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ADDRESS_DIALOG_ID:
			return createAddressDialog();
		}
		return null;
	}

	private Dialog createAddressDialog() {
		Dialog addressDialog = new Dialog(this);
		ListView listAddress = new ListView(this);
		ArrayAdapter<Address> adapter = new ArrayAdapter<Address>(this,
				android.R.layout.simple_list_item_1, addressDepartureResult);
		listAddress.setAdapter(adapter);
		OnItemClickListener listener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				coord = (Address) ((Object) view);
				dLatitude = coord.getLatitude();
				dLongitude = coord.getLongitude();
			}
			
		};
		listAddress.setOnItemClickListener(listener);
		addressDialog.setContentView(listAddress);
		return addressDialog;
	}

}
